package me.youline.dataServer.controller;

import javax.servlet.http.HttpServletResponse;

import me.youline.dataServer.ConfigConstants;
import me.youline.dataServer.entity.UserToken;
import me.youline.dataServer.exception.ReplicatedDataException;
import me.youline.dataServer.exception.UnExistPubMsgException;
import me.youline.dataServer.service.MsgPraiseRecordService;
import me.youline.dataServer.service.authentication.UserTokenService;
import me.youline.dataServer.service.front.SocialRelationDTOAdapter;
import me.youline.dataServer.service.support.MsgBuilder;
import me.youline.dataServer.utils.ResponseTypeOutputUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 消息点赞业务控制器
 * @author linhan
 *
 */
@Controller
@RequestMapping("/pubmsg/praise")
public class MsgPraiseController {

	private static final Logger logger = LoggerFactory
			.getLogger(MsgPraiseController.class);

	@Autowired
	private MsgPraiseRecordService praiseRecordService;

	@Autowired
	private UserTokenService userTokenService;

	@Autowired
	private SocialRelationDTOAdapter socialRelationAssembler;

	/**
	 * 为指定消息点赞
	 * 
	 * @param msgId
	 * @param tokenStr
	 * @param response
	 */
	@RequestMapping(value = "/{msgId}", method = RequestMethod.POST)
	public void praisePubMsg(
			@PathVariable Long msgId,@RequestParam(required=false) Long currentMsgId,
			@RequestParam(value = ConfigConstants.REQ_PARAM_APP_TOKEN_NAME) String tokenStr,
			HttpServletResponse response) {
		try {
			UserToken token = userTokenService.getUserTokenByTokenStr(tokenStr);
			int praiseNum = praiseRecordService.doMessagePraising(msgId,
					token.getUserId());
			response.setStatus(HttpServletResponse.SC_CREATED);
			ResponseTypeOutputUtils.renderJson(response, MsgBuilder.success()
					.addObj("praiseNum", praiseNum).addObj("msgId", msgId)
					.toJSONString());
		}  catch (ReplicatedDataException e) {
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			ResponseTypeOutputUtils.renderJson(response, MsgBuilder.error()
					.errorCode("200307").errorMsg("用户已经为该公告点赞").toJSONString());
			return;
		}catch(UnExistPubMsgException e){
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			ResponseTypeOutputUtils.renderJson(response, MsgBuilder.error()
					.errorCode("200302").errorMsg("公告已经不存在").toJSONString());
			return;
		}
		catch (Exception e) {
			logger.error(e.getMessage(), e);
			response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			ResponseTypeOutputUtils.renderJson(response, MsgBuilder.error()
					.errorCode("100001").errorMsg("点赞业务执行失败").toJSONString());
			return;
		}
	}

	/**
	 * 取消点赞
	 * 
	 * @param originMsgId
	 * @param tokenStr
	 * @param response
	 */
	@RequestMapping(value = "/{originMsgId}", method = RequestMethod.DELETE)
	public void deletePubMsgPraiseData(
			@PathVariable Long originMsgId,
			@RequestParam(value = ConfigConstants.REQ_PARAM_APP_TOKEN_NAME) String tokenStr,Long currentMsgId,
			HttpServletResponse response) {
		try {
			UserToken token = userTokenService.getUserTokenByTokenStr(tokenStr);
			boolean isDeleteOneRecord = praiseRecordService.deletePraiseRecord(originMsgId, token.getUserId());
			if(!isDeleteOneRecord){
				response.setStatus(HttpServletResponse.SC_NOT_FOUND);
				ResponseTypeOutputUtils.renderJson(response, MsgBuilder.error().errorCode("200310").errorMsg("用户未对当前公告点赞").toJSONString());
				return;
			}
			response.setStatus(HttpServletResponse.SC_OK);
			ResponseTypeOutputUtils.renderJson(response, MsgBuilder.success()
					.toJSONString());
			return;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			ResponseTypeOutputUtils.renderJson(response, MsgBuilder.error()
					.errorCode("100001").errorMsg("删除点赞记录业务执行失败"));
			return;
		}
	}

}
